//
//  WFComposeToolBar.swift
//  WangFangWeiBo
//
//  Created by 王方 on 16/7/18.
//  Copyright © 2016年 wf. All rights reserved.
//

import UIKit
//对这个bar上的五个部分定义枚举是很合适的
enum WFComposeToolBarBtnType:Int {
    //  图片
    case Picture = 0
    //  @
    case Mention = 1
    //  话题
    case Trend = 2
    //  表情
    case Emoticon = 3
    //  加号
    case Add = 4
}


//只有在9.0的时候才可以用这个控件
//@available(iOS 9.0, *)
class WFComposeToolBar: UIStackView {

    private var emotionBtn:UIButton?
    
    // MARK:  - 定义方法,对外提供修改表情图标
    func showEmotionIcon(isEmotion:Bool){
        //如果是表情键盘,那么显示系统图标
        //如果是系统键盘,那么显示表情图标
        if isEmotion {
        //显示键盘icon
            emotionBtn?.setImage(UIImage(named: "compose_keyboardbutton_background"), forState: .Normal)
            emotionBtn?.setImage(UIImage(named: "compose_keyboardbutton_background_highlighted"), forState: .Highlighted)
        } else {
            emotionBtn?.setImage(UIImage(named: "compose_emoticonbutton_background"), forState: .Normal)
            emotionBtn?.setImage(UIImage(named: "compose_emoticonbutton_background_highlighted"), forState: .Highlighted)
        
        }
    }
    
    //定义闭包进行按钮点击事件的回调
    var clickOnToolBarBtnClosure:((type:WFComposeToolBarBtnType)->())?
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // MARK:  - 初始化控件
    private func setUpUI(){
        //对于这个控件,设置颜色是没有用的,本质是个容器,外观属性也需要其内部的内容来决定
        axis = .Horizontal
        //子控件的填充方式,这个就是等分
        distribution = .FillEqually
    
        //添加子控件
        addChildBtn("compose_toolbar_picture", type: .Picture)
        addChildBtn("compose_mentionbutton_background", type: .Mention)
        addChildBtn("compose_trendbutton_background", type: .Trend)
      emotionBtn  = addChildBtn("compose_emoticonbutton_background", type: .Emoticon)
        addChildBtn("compose_add_background", type: .Add)
    }
    
    // MARK:  - 添加子控件按钮
    private func addChildBtn(imageName:String,type:WFComposeToolBarBtnType)->UIButton{
        let btn = UIButton()
        btn.tag = type.rawValue
        btn.addTarget(self, action: "btnAction:", forControlEvents: .TouchUpInside)
        
        btn.setImage(UIImage(named: imageName), forState: .Normal)
        btn.setImage(UIImage(named: "\(imageName)_highlighted"), forState: .Highlighted)
        btn.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: .Normal)
        btn.adjustsImageWhenHighlighted = false
        
        //对于这个stackview而言,添加控件的方式为
        addArrangedSubview(btn)
        return btn
    }
    
    // MARK:  - 点击单个的部分执行的方法
    @objc private func btnAction(btn:UIButton){
        //  通过枚举原始值获取枚举成员
        let type = WFComposeToolBarBtnType(rawValue: btn.tag)!
        //点击后闭包回调
        clickOnToolBarBtnClosure?(type: type)
    }
}
